home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.12 Dec 90 / CleanPict Source / OffscreenGrafPort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-25  |  2.7 KB  |  134 lines  |  [TEXT/KAHL]

  1. /*                                        OffscreenGrafPort.c                                */
  2. #ifdef DOCUMENTATION
  3.  
  4. Title        GrafPort Handler
  5.  
  6. Usage
  7.  
  8.         #include "OffscreenGrafPort.h"
  9.  
  10.         GrafPtr
  11.         CreateOSGrafPort(rect)
  12.         Rect        rect;
  13.         
  14.             Create a new GrafPort big enought to hold the Rect.
  15.             Note that its storage is not relocatable.  If there
  16.             isn't enough memory, CreateOSGrafPort will return
  17.             NIL and the GrafPort is -- obviously -- unusable.
  18.             If CreateOSGrafPort succeeds, the function does
  19.             SetPort to the new GrafPtr.
  20.  
  21.         void
  22.         CopyOSGrafPort(srcPort, dstPort, mask)
  23.         GrafPtr            srcPort;
  24.         GrafPtr            dstPort;
  25.         RgnHandle        mask;
  26.         
  27.             Copy the contents of the srcPort to the dstPort.
  28.             The entire port is copied.  Note: the picture is
  29.             stretched to fill the dstPort.  If this is
  30.             unsuitable, just call CopyBits yourself.
  31.         
  32.         void
  33.         DeleteOSGrafPort(theOSGrafPort)
  34.         GrafPtr            theOSGrafPort;
  35.         
  36.             Delete the GrafPort created by CreateOSGrafPort().
  37.         
  38. Normal Usage:
  39.  
  40.             GrafPtr            tempPort;
  41.             GrafPtr            oldPort;
  42.             
  43.             GetPort(&oldPort);
  44.             tempPort = CreateOSGrafPort(rect);
  45.             ... drawing commands ...
  46.             SetPort(oldPort);
  47.             /*
  48.              * Show my stuff
  49.              */
  50.             CopyOSGrafPort(tempPort, FrontWindow(), NIL);
  51.             DeleteOSGrafPort(tempPort);
  52.  
  53. acknowledgment
  54.  
  55.         Taken without much change from Mac TechNote 41
  56.         
  57. #endif
  58.  
  59. #include "OffscreenGrafPort.h"
  60. #ifndef width
  61. #define width(r)        ((r).right - (r).left)
  62. #define height(r)        ((r).bottom - (r).top)
  63. #endif
  64.  
  65. GrafPtr
  66. CreateOSGrafPort(box)
  67. Rect                box;
  68. {
  69.         GrafPtr                oldPort;
  70.         GrafPtr                newPort;
  71.         long                    rowBytes;
  72.         
  73.         /*
  74.          * Build an "empty" port big enough for our picture.
  75.          */
  76.         GetPort(&oldPort);
  77.         newPort = (GrafPtr) NewPtr(sizeof (GrafPort));
  78.         if (newPort == NIL || MemError() != noErr)
  79.             return (NIL);
  80.         OpenPort(newPort);
  81.         newPort->portRect = box;
  82.         newPort->portBits.bounds = box;
  83.         RectRgn(newPort->clipRgn, &box);
  84.         RectRgn(newPort->visRgn, &box);
  85.         rowBytes = ((width(box) + 15) >> 4) << 1;
  86.         newPort->portBits.rowBytes = rowBytes;
  87.         newPort->portBits.baseAddr =
  88.             NewPtr(rowBytes * (long) height(box));
  89.         if (newPort->portBits.baseAddr == NIL
  90.          || MemError() != noErr) {
  91.             SetPort(oldPort);
  92.             ClosePort(newPort);
  93.             DisposPtr(newPort);
  94.             return (NIL);
  95.         }
  96.         /*
  97.          * OpenPort did a SetPort to newPort
  98.          */
  99.         EraseRect(&box);
  100.         return (newPort);
  101. }
  102.  
  103. /*
  104.  * Copy between ports.  Note that the data is
  105.  * stretched to fill the entire dstPort.
  106.  */
  107. void
  108. CopyOSGrafPort(srcPort, dstPort, mask)
  109. GrafPtr            srcPort;
  110. GrafPtr            dstPort;
  111. RgnHandle        mask;
  112. {
  113.         CopyBits(
  114.             &(*srcPort).portBits, &(*dstPort).portBits,
  115.             &(*srcPort).portRect, &(*dstPort).portRect,
  116.             srcCopy,
  117.             mask
  118.         );
  119. }
  120.  
  121. /*
  122.  * Dispose of the port.
  123.  */
  124. void
  125. DeleteOSGrafPort(oldPort)
  126. GrafPtr            oldPort;
  127. {
  128.         ClosePort(oldPort);
  129.         DisposPtr((*oldPort).portBits.baseAddr);
  130.         DisposPtr((Ptr) oldPort);
  131. }
  132.  
  133.  
  134.